package com.dkd.manage.service.impl;

import java.util.List;
import com.dkd.common.utils.DateUtils;
import com.dkd.manage.domain.Contract;
import com.dkd.manage.domain.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.dkd.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.dkd.manage.domain.TbOrderDetail;
import com.dkd.manage.mapper.OrderMapper;
import com.dkd.manage.domain.Order;
import com.dkd.manage.service.IOrderService;

/**
 * 订单管理Service业务层处理
 * 
 * @author lengxx
 * @date 2025-03-27
 */
@Service
public class OrderServiceImpl implements IOrderService 
{
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ContractServiceImpl contractService;
    /**
     * 查询订单管理
     * 
     * @param id 订单管理主键
     * @return 订单管理
     */
    @Override
    public Order selectOrderById(Long id)
    {
        return orderMapper.selectOrderById(id);
    }

    /**
     * 查询订单管理列表
     * 
     * @param order 订单管理
     * @return 订单管理
     */
    @Override
    public List<Order> selectOrderList(Order order)
    {
        return orderMapper.selectOrderList(order);
    }

    /**
     * 新增订单管理
     * 
     * @param order 订单管理
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertOrder(Order order)
    {
        order.setCreateTime(DateUtils.getNowDate());
        int rows = orderMapper.insertOrder(order);
        insertTbOrderDetail(order);

        //当订单已经结清的时候更新合同状态
        String orderStatus = order.getOrderStatus();
        if (orderStatus.equals("3")) {
            Contract cont = new Contract();
            cont.setId(order.getContractId());
            cont.setTotalAmount(order.getTotalAmount());
            cont.setContractStatus("3");
            contractService.updateContract(cont);
        }


        return rows;
    }

    /**
     * 修改订单管理
     * 
     * @param order 订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOrder(Order order)
    {
        order.setUpdateTime(DateUtils.getNowDate());
        orderMapper.deleteTbOrderDetailByOrderId(order.getId());
        insertTbOrderDetail(order);
        //当订单已经结清的时候更新合同状态
        String orderStatus = order.getOrderStatus();
        if (orderStatus.equals("3")) {
            Contract cont = new Contract();
            cont.setId(order.getContractId());
            cont.setTotalAmount(order.getTotalAmount());
            cont.setContractStatus("3");
            contractService.updateContract(cont);
        }

        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除订单管理
     * 
     * @param ids 需要删除的订单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrderByIds(Long[] ids)
    {
        orderMapper.deleteTbOrderDetailByOrderIds(ids);
        return orderMapper.deleteOrderByIds(ids);
    }

    /**
     * 删除订单管理信息
     * 
     * @param id 订单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrderById(Long id)
    {
        orderMapper.deleteTbOrderDetailByOrderId(id);
        return orderMapper.deleteOrderById(id);
    }

    /**
     * 新增订单详情信息
     * 
     * @param order 订单管理对象
     */
    public void insertTbOrderDetail(Order order)
    {
        List<TbOrderDetail> tbOrderDetailList = order.getTbOrderDetailList();
        Long id = order.getId();
        if (StringUtils.isNotNull(tbOrderDetailList))
        {
            List<TbOrderDetail> list = new ArrayList<TbOrderDetail>();
            for (TbOrderDetail tbOrderDetail : tbOrderDetailList)
            {
                tbOrderDetail.setOrderId(id);
                list.add(tbOrderDetail);
            }
            if (list.size() > 0)
            {
                orderMapper.batchTbOrderDetail(list);
            }
        }
    }


    @Override
    public List<OrderVo> selectOrderVoList(Order order) {
        return orderMapper.selectOrderVoList(order);
    }
}
